package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 讲师 前端控制器
 * @author 孟繁钦
 * @date 2021-12-07
 */

@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduService/teacher")
@CrossOrigin
public class EduTeacherController {

    /**
     * 知识点
     * @ApiOperatio Swagger 接口方法描述
     * @Api(tags = "讲师管理") swagger 接口描述
     *
     * @rest风格
     * 查询 get
     * 添加 put
     * 删除 delete
     *
     * @mybatisPlus 语法
     *      * eq 等于
     *      * ge 大于
     *      * le 小于
     *      * like 模糊查询
     * @RequestBody
     *  !required = false 可以表示没有这个参数值 默认为 true
     *  :前端使用JSON来传递数据,把JSON数据封装到对应的对象里面
     *  需要使用post请求,get无法获取数据
     * @ResponseBody
     *  :主要用于返回JSON数据
     */

    //把service注入
    @Autowired
    private EduTeacherService teacherService;


    //1.查询讲师表所有数据
    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher() {
        //调用service 方法实现查询所有的操作
        List<EduTeacher> list = teacherService.list(null);

        return R.ok().data("items", list);
    }


    //2.讲师逻辑删除 带{}必须携带此参数 id需要通过 *路径传输*
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    //3.分页查询讲师的方法 *current*当前页 *limit* 每页显示数据行数
    @ApiOperation(value = "讲师查询分页")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable Long current,
                             @PathVariable Long limit){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法实现分页 底层封装 所有数据封装到 *pageTeacher*
        teacherService.page(pageTeacher, null);
        /*总记录数*/
        long total = pageTeacher.getTotal();
        /*分页之后的数据集合*/
        List<EduTeacher> records = pageTeacher.getRecords();
        //返回封装的data 数据
        return R.ok().data("total",total).data("rows",records);
    }



    //4.带条件查询分页
    @ApiOperation(value = "根据条件分页查询")
    @PostMapping("pageTeacherConditionr/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){

         //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构建查询条件对象
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        //动态sql查询
        if(null!=teacherQuery){
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();

            if(!StringUtils.isEmpty(name)){
                //模糊查询名字
                wrapper.like("name",name);
            }
            if(!StringUtils.isEmpty(level)){
                wrapper.eq("level",level);
            }

            if(!StringUtils.isEmpty(begin)){
                wrapper.ge("gmt_create",begin);
            }

            if(!StringUtils.isEmpty(end)){
                wrapper.le("gmt_modified",end);
            }
            //根据时间降序
            wrapper.orderByDesc("gmt_create");
        }

        //调用方法实现条件查询带分页功能
        teacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();/*总记录数*/
        List<EduTeacher> records = pageTeacher.getRecords();/*分页数据集合*/
        return R.ok().data("total",total).data("rows",records);
    }

    //5.添加讲师的方法
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        return save ? R.ok():R.error();
    }

    //6.根据讲师id查询
    @ApiOperation(value = "根据讲师id查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher byId = teacherService.getById(id);
        return R.ok().data("teacher",byId);
    }

    //7.讲师修改
    @ApiOperation(value = "根据讲师id修改信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean falg = teacherService.updateById(eduTeacher);
        return falg ? R.ok() : R.error();
    }
}

